<?php
namespace Admin\Form;
use Common\Model\UserModel;
use Common\Model\UserMoneyModel;
use Common\Form\BaseForm;
class UserForm extends BaseForm {
    public static function newSelf()
    {
        return new self();
    }
    protected $data;
    protected $action;
    protected $return_type;
    public function __construct()
    {
        set_time_limit(0);
        parent::__construct();
        $this->return_type='ajax';
        $this->data['register']=[
            'user_id'=>'user_id',
            'username'=>'username',
            'realname'=>'realname',
            'password'=>'password',
            'password2'=>'password2',
            'twopassword'=>'twopassword',
            'twopassword2'=>'twopassword2',
            'phone'=>'phone',
            'email'=>'email',
            'idcard'=>'idcard',
            'address'=>'address',
            'wechat'=>'wechat',
            'qq'=>'qq',
            'sex'=>'sex',
            'tel'=>'tel',
            'tuiname'=>'tuiname',
            'jiename'=>'jiename',
            'centername'=>'centername',
            'quyu'=>'quyu',
            'leavel'=>'leavel'
        ];
        $this->data['search']=[
            'username'=>'username'
        ];
        $this->data['update']=[
            'id'=>'id',
            'realname'=>'realname',
            'password'=>'password',
            'twopassword'=>'twopassword',
            'phone'=>'phone',
            'email'=>'email',
            'idcard'=>'idcard',
            'address'=>'address',
            'wechat'=>'wechat',
            'qq'=>'qq',
            'tel'=>'tel',
            'honor_leavel'=>'honor_leavel'
        ];
    }
    public function search($input)
    {
        $this->return_type='error';
        $this->action='search';
        $this->__toInitialize($input);
        $info=$this->findUserByUserName('username', '用户不存在');
        return $info;
    }
    
    public function update($input)
    {
        $this->action='update';
        $this->__toInitialize($input);
        $this->emptyVerification('id', '请选择用户');
        $model=UserModel::newSelf();
        if ($this->getDataVal('password')) {
            $this->addDataVal('password',$model->passwordEncrypt($this->getDataVal('password')));
        }else{
            $this->delDataVal('password');
        }
        if ($this->getDataVal('twopassword')) {
            $this->addDataVal('twopassword',$model->twoPasswordEncrypt($this->getDataVal('twopassword')));
        }else{
            $this->delDataVal('twopassword');
        }
        $res=$model->where(['id'=>$this->getDataVal('id')])->save($this->getAllData());
        $this->resReturn($res, '修改成功', '修改失败');
        
    }
    public function register($input)
    {
        $this->action='register';
        
        $this->__toInitialize($input);
        $this->emptyVerification('user_id','系统繁忙请稍后再试');
        $this->emptyVerification('username','请输入用户名');
        $this->emptyVerification('realname','请输入姓名');
        $this->emptyVerification('password','请输入一级密码');
        $this->emptyVerification('password2','请确认一级密码');
        $this->emptyVerification('twopassword','请输入二级密码');
        $this->emptyVerification('twopassword2','请确认二级密码');
        $this->emptyVerification('phone','请输入手机号码');
        $this->emptyVerification('sex','请选择性别');
        $this->emptyVerification('tuiname','请输入推荐人');
        $this->emptyVerification('jiename','请输入安置人');
        $this->emptyVerification('centername','请输入报单中心');
        $this->emptyVerification('quyu','请选择区域');
        $this->emptyVerification('leavel','请选择级别');

        $user=UserModel::newSelf()->findUserByUsername($this->getDataVal('username'));
        if ($user) {
            $this->returnResult('用户名重复请重新输入');
        }
        $model=UserModel::newSelf();

        $this->equalVerification('password', 'password2', '一级密码与重复密码不相同');
        $this->addDataVal('password', $model->passwordEncrypt($this->getDataVal('password')));
        $this->delDataVal('password2');
        
        $this->equalVerification('twopassword', 'twopassword2', '一级密码与重复密码不相同');
        $this->addDataVal('twopassword', $model->twoPasswordEncrypt($this->getDataVal('password')));
        $this->delDataVal('twopassword2');
        
        $this->inArrayVerification('sex',['1','2'],'性别信息有误');
        $this->inArrayVerification('quyu',['1','2'],'区域信息有误');
        $this->inArrayVerification('leavel',['1','2'],'级别信息有误');

        $this->nequalVerificationManual('leavel','1','此级别暂未开放');

        $tui_user=$this->findUserByUsername('tuiname','推荐人不存在');
        $this->nequalVerificationManual($tui_user['status'],'0','推荐人未激活');
        $this->nequalVerificationManual($tui_user['lock'],'1','推荐人被封号');
        $this->delDataVal('tuiname');
        $this->addDataVal('tui_id', $tui_user['id']);

        $jie_user=$this->findUserByUsername('jiename','接点人不存在');
        $this->nequalVerificationManual($jie_user['status'],'0','接点人未激活');
        $this->nequalVerificationManual($jie_user['lock'],'1','接点人被封号');
        $another=$model->getDownUserByUpAndQuyu($jie_user['id'],$this->getDataVal('quyu'));
        if ($another) {
            $this->returnResult('此区域已经存在用户');
        }
        $this->delDataVal('jiename');
        $this->addDataVal('jie_id', $jie_user['id']);

        $center_user=$this->findUserByUsername('centername','报单中心不存在');
        $this->nequalVerificationManual($center_user['status'],'3','此用户并非报单中心');
        $this->nequalVerificationManual($center_user['lock'],'1','报单中心被封号');
        $this->delDataVal('centername');
        $this->addDataVal('center_id', $center_user['id']);
        $this->addDataVal('reg_time',time());
        
        $res=$model->add($this->getAllData());
        $this->changeMaxCeng($jie_user['id'],$this->getDataVal('quyu'));
        UserMoneyModel::newSelf()->initializationMoneyInfo($res);
        
        $this->resReturn($res,'注册成功','系统繁忙请重新注册');
        
    }
    public function changeMaxCeng($up_id,$quyu)
    {
        $model=UserModel::newSelf();
        $up=$model->findUpUser($up_id);
        foreach ($up as $k=>$v)
        {
            if ($up[$k-1]) {
                $quyu=$up[$k-1]['quyu'];
            }
            
            if (isset($v['ceng'.$quyu])&&$v['ceng'.$quyu]<$k+1) {
                $model->where(['id'=>$v['id']])->setField('ceng'.$quyu,$k+1);
            }
        }
        
    }
    public function randUserName()
    {
        $username='A'.rand(100000,999999);
        $user=UserModel::newSelf()->findUserByUsername($username);
        if ($user) {
            $this->randUserName();
        }
        $this->returnResult($username,true);
    }
    
}